package cc.eddic.pratice.programmingparadigms.emp01array.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp01array.EmployeeStatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class StatisticsScwImpl extends ReaderGcsImpl implements EmployeeStatisticsService {

    private static final Logger log = LoggerFactory.getLogger(StatisticsScwImpl.class);


    /**
     * 从excel读取数据
     *
     * @param filename excel 文件名
     * @return Employee 数组
     */
    public Employee[] readFromExcel(String filename) {
        return new Employee[]{
                new Employee("曾奕哲", "男", "SIE", "General Staff"),
                new Employee("李馨", "女", "JP APP2 GP1", "软件工程师"),
                new Employee("谭雨晴", "女", "PLM Service Dept", "软件工程师"),
                new Employee("韩睿洁", "女", "SSS", "Salesforce解决方案工程师"),
                new Employee("李惠", "女", "ERP", "SAP运维开发工程师"),
                new Employee("王巧家", "男", "ERP", "SAP运维开发工程师"),
                new Employee("孙晨玮", "男", "Enterprise Common Service Devision", "SAP运维开发工程师"),
                new Employee("崔相元", "男", "Procurement Solution", "云计算开发工程师")
        };

    }


    /**
     * 按照性别统计
     *
     * @param employees
     * @return 统计结果 数组
     */
    public Result[] pivotByGender(Employee[] employees) {
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (result == null) {
                    break;
                }
                if (result.getKey().equals(employee.getGender())) {
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                    break;
                }
            }
            if (!isFound) {
                results[index++] = new Result(employee.getGender(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++) {
            newResults[i] = results[i];
        }
        bubbleSort(newResults);
        return newResults;

    }


    /**
     * 按照部门统计
     *
     * @param employees
     * @return统计结果 数组
     */
    public Result[] pivotByDepartment(Employee[] employees) {
        //TODO：职位统计
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (result == null) {
                    continue;
                }
                if (result.getKey().equals(employee.getDepartment())) {
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                    break;
                }
            }
            if (!isFound) {
                results[index++] = new Result(employee.getDepartment(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++) {
            newResults[i] = results[i];
        }
        bubbleSort(newResults);
        return newResults;
    }

    /**
     * 按照岗位统计
     *
     * @param employees
     * @return 统计结果 数组
     */
    public Result[] pivotByJobTitle(Employee[] employees) {
        //TODO：职位统计
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (result == null) {
                    break;
                }
                if (result.getKey().equals(employee.getJobTitle())) {
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                    break;
                }
            }
            if (!isFound) {
                results[index++] = new Result(employee.getJobTitle(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++) {
            newResults[i] = results[i];
        }
        bubbleSort(newResults);
        return newResults;

    }


     void bubbleSort(final Result[] results) {
        for (int i = 0; i < results.length; i++) {
            for (int j = 0; j < results.length - i - 1; j++) {
                if (results[j].getCount() < results[j + 1].getCount()) {
                    Result temp = results[j];
                    results[j] = results[j + 1];
                    results[j + 1] = temp;
                }
            }
        }
    }
}
